/*
 * Copyright (c) 2011 Ball State University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * NOTE:
 * Derivative work based on a sample project from CommonsWare, LLC
 * From _The Busy Coder's Guide to Advanced Android Development_
 *		http://commonsware.com/AdvAndroid
 */

package edu.bsu.android.gtv.apps.bconnected.ui;

import edu.bsu.android.gtv.apps.bconnected.R;
import edu.bsu.android.gtv.apps.bconnected.objects.Event;
import edu.bsu.android.gtv.apps.bconnected.ui.EventFragment;
import edu.bsu.android.gtv.apps.bconnected.ui.base.BaseActivity;
import edu.bsu.android.gtv.apps.bconnected.util.Constants;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;

/**
 * A multi-pane activity, consisting of a {@link MonthFragment} and a {@link EventFragment} 
 * @author Kyle Parker, University Computing Services
 */
public class EventMultiPaneActivity extends BaseActivity implements 
	MonthFragment.OnMonthListener,
	EventFragment.OnEventListener {

    private FragmentManager mFragmentManager;
    private MonthFragment mMonthFragment;
	private Activity mActivity;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event);

		mActivity = this;
        getActivityHelper().setupActionBar(false, "Campus Events");
        
        mFragmentManager = getFragmentManager();
        mMonthFragment = (MonthFragment) mFragmentManager.findFragmentById(R.id.fragment_month_list); 
        mMonthFragment.setOnMonthListener(this);
        mMonthFragment.enablePersistentSelection();
    }

    /**
     * Create a fragment transaction and load the events for the selected month
     * loadEvents found in {@link EventFragment}
     * @param position
     */
	public void addEventFragment(int position) {
		EventFragment events = (EventFragment) mFragmentManager.findFragmentById(R.id.fragment_container_event_list);
		FragmentTransaction ft = mFragmentManager.beginTransaction();
		
		if (events == null) {
			events = new EventFragment(true);
			events.setOnEventListener(this);
			
			ft.add(R.id.fragment_container_event_list, events)
				.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
				.addToBackStack(null)
				.commit();
		}
			
		events.startActivity(position);
	}
	
	/** 
	 * @see edu.bsu.android.apps.gtv.mybsu.ui.MonthFragment.OnMonthListener#onMonthSelected(int)
	 * When the user taps a month, the OnMonthListener in the MonthFragment will fire, calling this method
	 * The addEventsFragment method will be called, creating a fragment transaction and loading the events based on the month selected
	 * @param position
	 */
	public void onMonthSelected(int position) { 
		addEventFragment(position);
	}
	
	/**
	 * @see edu.bsu.android.apps.gtv.mybsu.ui.EventFragment.OnEventListener#onEventSelected(edu.bsu.android.apps.gtv.mybsu.objects.Event)
	 * When an event is selected, start a new fragment to display the event information
	 * @param event
	 */
	public void onEventSelected(Event event) {
		Intent intent = new Intent(mActivity, EventInfoActivity.class);
		intent.putExtra(Constants.EXTRA_EVENT_ID, event.getUniversityCalendarID());
		
		mActivity.startActivity(intent);
	}
}
